<?php
	/*
	 * Home_model
	 * @author: Tran Van Thanh
	 * Email  : thanhtran@vietnambiz.com
	 * Date:	30/08/2011
	 */
class Home_model extends MY_Model{

	function __construct()
	{
		parent::__construct();
		$this->_table = 'customers';
	}
	
	function get_slideshows()
	{
		$where = array(
    		'status'	=> 'yes'
    	);    
        
        $this->db->select()
                 ->from("slideshows")               
                 ->where($where); 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result;   
	}
			
	function get_company()
	{
		$this->db->order_by('order ASC');
		$this->db->where(array('status'=>'yes'));
		return $this->db->get('company')->result();
	}
	
	function count_product_by_company($company_id = null)
	{		
        $where = array(
    		'id_company'	=> $company_id
    	);    
        
        $this->db->select("count(id) as sum_products")
                 ->from("products")               
                 ->where($where); 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result;        
	}
		
	function get_all_products($status = "",$offset = 0,$limit = 12)
	{
		if($status == 'hot')
		{			    		
    		$this->db->where('a.hot', 'yes');    		
		}
		else if($status == 'best_sales')
		{		
    		$this->db->where('a.best_sales','yes');    		
		}
        
        $data = array();
        
        $num_rows = $this->db->select('count(a.id)as cnt')
                 ->from("products as a")
                 ->where('status','yes')  
                 ->get()->row()->cnt;
        $data['num_rows']   =   $num_rows;
		        
        $this->db->select('a.*')
                 ->from("products as a")
                 ->where('status','yes')  
                 ->order_by('a.date_create DESC')
                 ->limit($limit,$offset);                 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();
        $data['rows']   =   $result;        
        return $data;  
	}
			
	function get_images_products_by_product_id($product_id = null)
	{
		$this->db->select()
                 ->from("images_products")
                 ->where('id_product',$product_id)
                 ->order_by('date_create DESC'); 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result; 
	}
	
	function get_support()
	{
		$this->db->select()
                 ->from("support")
                 ->where('status','yes');                 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result; 
	}
	
	function get_utilities()
	{
		$this->db->select()
                 ->from("utilities")
                 ->where('status','yes')
                 ->order_by('order');                 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result; 
	}
	
	function count_products_by_utilities_id($utilities_id = null)
	{
		$where = array(
    		'id_utilities'	=> $utilities_id
    	);    
        
        $this->db->select("count(id) as sum_products")
                 ->from("products")               
                 ->where($where); 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result;  
	} 
	
	function count_all_products_by_price($from_price = null,$to_price = null)
	{
		if($from_price == 0 && $to_price == 5)
		{
			$this->db->where('price <', 5000000);	
		}
		else if($from_price == 5 && $to_price == 7)
		{
			$this->db->where("price BETWEEN 5000000 AND 7000000");			
		}
		else if($from_price == 7 && $to_price == 10)
		{			
			$this->db->where("price BETWEEN 7000000 AND 10000000");
		}
		else if($from_price == 10 && $to_price == 12)
		{
			$this->db->where("price BETWEEN 10000000 AND 12000000");
		}
		else if($from_price == 12 && $to_price == 0)
		{
			$this->db->where('price >', 12000000);
		}
		
		$this->db->select("count(id) as sum_products")
                 ->from("products");
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result; 
	}
	
	function count_products_by_power($from_power = null, $to_power = null)
	{
		if($from_power == 0 && $to_power == 9)
		{
			$where = array(
				'status'			=> 'yes',
				'cool_power <='		=> 9000
			);		
		}
		else if($from_power == 9 && $to_power == 10)
		{
			$where = array(
				'status'			=> 'yes',
				'cool_power >'		=> 9000,
				'cool_power <=' 	=> 10000
			);			
		}
		else if($from_power == 10 && $to_power == 12)
		{
			$where = array(
				'status'			=> 'yes',
				'cool_power >'		=> 10000,
				'cool_power <=' 	=> 12000
			);				
		}
		else if($from_power == 12 && $to_power == 13)
		{						
			$where = array(
				'status'			=> 'yes',
				'cool_power >'		=> 12000,
				'cool_power <=' 	=> 13000
			);		
		}
		else if($from_power == 13 && $to_power == 18)
		{			
			$where = array(
				'status'			=> 'yes',
				'cool_power >'		=> 13000,
				'cool_power <=' 	=> 18000
			);			
		}
		else if($from_power == 18 && $to_power == 24)
		{			
			$where = array(
				'status'			=> 'yes',
				'cool_power >'		=> 18000,
				'cool_power <=' 	=> 24000
			);			
		}
		else if($from_power == 24 && $to_power == 30)
		{			
			$where = array(
				'status'			=> 'yes',
				'cool_power >'		=> 24000,
				'cool_power <=' 	=> 30000
			);	
		}
				
		
		$this->db->select("count(id) as sum_products")
                 ->from("products")
                 ->where($where);
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result; 
	}
	
	function get_news($type = null)
	{		
		if($type == 'works')
		{
			$this->db->limit(3);
			$where = array(
				'a.news_works'			=> 'yes',			
				'a.news_recruitment' 	=> 'no',
				'a.status'				=> 'yes'
			);
		}
		else if($type  == 'all')
		{
			$this->db->limit(6);
			$where = array(
				'a.news_works'			=> 'no',
				'a.news_recruitment' 	=> 'no',
				'a.status'				=> 'yes'
			);
		}
		
		$this->db->select()
                 ->from("articles as a")                 
                 ->where($where)                 
                 ->order_by("a.date_create DESC");
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result;		
	}
	
	function get_images_news($articles_id = null)
	{
		$this->db->select()
                 ->from("images_news")
                 ->where('id_articles',$articles_id)
                 ->order_by('date_create DESC'); 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result; 
	}
	
	function get_questions($limit = null)
	{
		$where = array(
			'a.status'	=> 'yes'
		);
		$this->db->select("a.*,b.fullname,c.contents as answers")
                 ->from("questions as a")
                 ->join("customers as b","a.id_customer = b.id")
                 ->join("answers as c","a.id = c.id_question")
                 ->limit($limit)                 
                 ->order_by('a.date_create DESC'); 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result; 
	}
	
	function get_partners()
	{
		$this->db->select()
                 ->from("partners")
                 ->where('status','yes');                 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result; 
	}
	
	function get_table_price_newest()
	{
		$this->db->select()
                 ->from("table_price")
                 ->where(array('status'=>'yes'))
                 ->order_by('date_create DESC'); 
        $q = $this->db->get();
        $result = $q->row();
        $q->free_result();        
        return $result; 
	}
	
	function get_detail_file($link = '')
	{
		$where = array('name_link' => $link,'status' => 'yes');
		$this->db->select()
                 ->from("table_price")
                 ->where($where)
                 ->order_by('date_create DESC'); 
        $q = $this->db->get();
        $result = $q->row();
        $q->free_result();        
        return $result; 
	}
    
    function get_by_type($type_id,$offset=0,$limit=12)
    {
        $data = array();
        $where = array(
            'status' => 'yes',
            'id_product_type'       => $type_id
        );
        $num_rows = $this->db->select("count(id)as cnt")
                        ->from("products")
                        ->where($where)
                        ->get()->row()->cnt;
        $data['num_rows']   =   $num_rows;
        $rows = $this->db->select()->from("products")->where($where)->limit($limit,$offset)->get()->result();
        $data['rows'] = $rows;
        return $data;
    }
}
